package com.terrace.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.terrace.core.common.model.ResultData;
import com.terrace.db.system.mapper.entity.Lang;
import com.terrace.db.system.service.LangService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.web.annotation.Logs;
import com.terrace.web.constant.GlobalsConstant;
import com.terrace.web.model.DataGrid;

/**
 * 常用语
 * @author jiangyg
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/lang")
public class LangController {
	
	private static final String MESSAGE_TYPE = "常用语";
	
	@Autowired
	private LangService langService;
	
	/**
	 * 默认管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView langList(HttpServletRequest request){
		return new ModelAndView("system/lang/langList");
	}
	
	/**
	 * 数据表格
	 * @param request
	 * @param pagination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "datagrid")
	@RequiresPermissions("system:lang:view")
	public DataGrid<Lang> datagrid(Lang lang, Pagination<Lang> pagination, HttpServletRequest request) {
		DataGrid<Lang> dataGrid = new DataGrid<Lang>();
		dataGrid.setPagination(langService.selectLangForPagination(pagination, lang));
		return dataGrid;
	}
	
	/**
	 * 新增或者修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(@RequestParam(value = "id", required = false) Integer langId,
			HttpServletRequest request) {
		if (langId != null) {
			Lang lang = langService.selectLangById(langId);
			request.setAttribute("lang", lang);
		}
		return new ModelAndView("system/lang/lang");
	}
	
	/**
	 * 保存语言
	 * @param lang
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequestMapping(params = "saveOrUpdate")
	@RequiresPermissions("system:lang:save")
	@Logs(depict = "常用语保存", type = GlobalsConstant.LogTypes.SYS_ICON)
	public ResultData saveOrUpdate(Lang lang, HttpServletRequest request) {
		ResultData rd = new ResultData();
		if (lang.getId() == null) {
			langService.saveLangSelective(lang);
			rd.successObjs("10001", MESSAGE_TYPE);
		} else {
			Lang temp = langService.selectLangById(lang.getId());
			if (temp == null) {
				return rd.failObjs("13103", MESSAGE_TYPE);
			}
			langService.updateLangByIdSelective(lang);
			rd.successObjs("10002", MESSAGE_TYPE);
		}
		return rd;
	}
	
	/**
	 * 删除语言
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "delete")
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequiresPermissions("system:lang:delete")
	@Logs(depict = "删除常用语", type = GlobalsConstant.LogTypes.SYS_ICON)
	public ResultData delete(@RequestParam(value = "id") Integer langId, HttpServletRequest request) {
		ResultData rd = new ResultData();
		Lang lang = langService.selectLangById(langId);
		if (lang == null) {
			return rd.failObjs("13103", MESSAGE_TYPE);
		}
		int result = langService.deleteLangById(langId);
		if (result != 1) {
			rd.failObjs("13102", MESSAGE_TYPE);
		} else {
			rd.successObjs("10003", MESSAGE_TYPE);
		}
		return rd;
	}

}
